package com.rupeng.hibernate.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class DataBaseImpl implements DataBase {

	public String createInsertSQL(Properties properties, Object obj) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("insert into ");
		String objName = obj.getClass().getName();
		String table = properties.getProperty(objName);
		sql.append(table);
		sql.append("(");
		
		Map columns = new HashMap();
		
		BeanInfo bi = null;
		try {
			bi = Introspector.getBeanInfo(obj.getClass(), Object. class );
			PropertyDescriptor[] props = bi.getPropertyDescriptors();
	        for ( int i=0;i<props.length;i++){
//System.out.println(props[i].getName()+ "=" + props[i].getReadMethod().invoke(obj, null ));
	        	String column = (String) properties.get(props[i].getName());
	        	Object value = props[i].getReadMethod().invoke(obj, null );
	        	columns.put(column, value);
	        	
	        	sql.append(column);
	        	if(i != props.length - 1) {
	        		sql.append(", ");
	        	}
	        }
	        sql.append(")");
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		sql.append(" values(");
		try {
			bi = Introspector.getBeanInfo(obj.getClass(), Object. class );
			PropertyDescriptor[] props = bi.getPropertyDescriptors();
	        for ( int i=0;i<props.length;i++){
	        	String column = (String) properties.get(props[i].getName());
	        	Object value = columns.get(column);
//System.out.println("TYPE:" + Integer.TYPE);
//System.out.println("TYPE:" + props[i].getWriteMethod().getParameterTypes()[0] + "--" + props[i].getName());
	        	Class type = props[i].getWriteMethod().getParameterTypes()[0];
	        	
	        	if("int".equals(type) || Integer.TYPE == type) {
	        		sql.append(value);
	        	} else {
	        		sql.append("'");
	        		sql.append(value);
	        		sql.append("'");
	        	}	        	
	        	
	        	if(i != props.length - 1) {
	        		sql.append(", ");
	        	}
	        }
	        sql.append(")");
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sql.toString();
	}
	
	public Statement createStat(Connection conn) {
		Statement stat = null;
		try {
			stat = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stat;
	}

	public void closeStat(Statement stat) {
		try {
			if(stat != null) {
				stat.close();
				stat = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ResultSet executeQuery(String sql, Statement stat) {
		ResultSet rs = null;
		try {
			rs = stat.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public int executeInsert(String sql, Connection conn) {
		int updataNum = 0;
		Statement stat = null;
		try {
			stat = conn.createStatement();
			updataNum = stat.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updataNum;
	}
}
